﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Truron.Purchase.Utility.RedisHelper
{
    public class RedisBase
    {
        public static bool ContainsKey(string key)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                return redis.ContainsKey(key);
            }
        }

        #region -- Item --
        /// <summary>
        /// 设置单体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public static bool Item_Set<T>(string key, T t)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.Set<T>(key, t, new TimeSpan(1, 0, 0));
            }
        }

        public static long Item_Inc(string key, uint skip)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.Increment(key, skip);
            }
        }

        /// <summary>
        /// 获取单体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Item_Get<T>(string key) where T : class
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                return redis.Get<T>(key);
            }
        }

        /// <summary>
        /// 移除单体
        /// </summary>
        /// <param name="key"></param>
        public static bool Item_Remove(string key)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.Remove(key);
            }
        }

        /// <summary>
        /// 设置过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static bool Item_SetExpire(string key, DateTime datetime)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.ExpireEntryAt(key, datetime);
            }
        }

        /// <summary>
        /// 设置过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static bool Item_SetExpire(string key, TimeSpan timeSpan)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.ExpireEntryIn(key, timeSpan);
            }
        }

        #endregion

        #region -- List --

        public static void List_Add<T>(string key, params T[] ts)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                var redisTypedClient = redis.As<T>();
                foreach (var t in ts)
                    redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
            }
        }

        public static bool List_Remove<T>(string key, T t)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                var redisTypedClient = redis.As<T>();
                return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0;
            }
        }
        public static void List_RemoveAll<T>(string key)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                var redisTypedClient = redis.As<T>();
                redisTypedClient.Lists[key].RemoveAll();
            }
        }

        /// <summary>
        /// 修剪List
        /// </summary>
        /// <param name="key"></param>
        /// <param name="size">保留的条数</param>
        /// <returns></returns>
        public static void List_Trim(string key, int size)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                redis.Lists[key].Trim(0, size);
            }
        }

        public static long List_Count(string key)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                return redis.GetListCount(key);
            }
        }

        public static List<T> List_GetRange<T>(string key, int start, int count)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                var c = redis.As<T>();
                return c.Lists[key].GetRange(start, start + count - 1);
            }
        }

        public static List<T> List_GetList<T>(string key)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                var c = redis.As<T>();
                return c.Lists[key].GetRange(0, c.Lists[key].Count);
            }
        }

        /// <summary>
        /// 设置缓存过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        public static void List_SetExpire(string key, DateTime datetime)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                redis.ExpireEntryAt(key, datetime);
            }
        }
        #endregion

        #region -- Hash --
        /// <summary>
        /// 判断某个数据是否已经被缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public static bool Hash_Exist<T>(string key, string dataKey)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                return redis.HashContainsEntry(key, dataKey);
            }
        }

        public static bool Hash_Set<T>(string key, Guid dataKey, T t)
        {
            return Hash_Set<T>(key, dataKey.ToString(), t);
        }
        public static bool Hash_Set<T>(string key, int dataKey, T t)
        {
            return Hash_Set<T>(key, dataKey.ToString(), t);
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public static bool Hash_Set<T>(string key, string dataKey, T t)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
                return redis.SetEntryInHash(key, dataKey, value);
            }
        }
        /// <summary>
        /// 移除hash中的某值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public static bool Hash_Remove(string key, string dataKey)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.RemoveEntryFromHash(key, dataKey);
            }
        }
        /// <summary>
        /// 移除整个hash
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public static bool Hash_Remove(string key)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                return redis.Remove(key);
            }
        }
        /// <summary>
        /// 从hash表获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public static T Hash_Get<T>(string key, string dataKey)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                string value = redis.GetValueFromHash(key, dataKey);
                return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(value);
            }
        }

        public static List<T> Hash_GetList<T>(string key, params Guid[] dataKeys)
        {
            List<string> keyStrings = new List<string>();
            foreach (var item in dataKeys) keyStrings.Add(item.ToString());
            return Hash_GetList<T>(key, keyStrings.ToArray());
        }

        public static List<T> Hash_GetList<T>(string key, params string[] dataKeys)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                List<T> result = new List<T>();
                var list = redis.GetValuesFromHash(key, dataKeys);
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item))
                        result.Add(ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item));
                }
                return result;
            }
        }
        /// <summary>
        /// 获取整个hash的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<T> Hash_GetAll<T>(string key)
        {
            using (IRedisClient redis = RedisContext.GetReadOnlyClient())
            {
                var list = redis.GetHashValues(key);
                if (list != null && list.Count > 0)
                {
                    List<T> result = new List<T>();
                    foreach (var item in list)
                    {
                        var value = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
                        result.Add(value);
                    }
                    return result;
                }
                return null;
            }
        }
        /// <summary>
        /// 设置缓存过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        public static void Hash_SetExpire(string key, DateTime datetime)
        {
            using (IRedisClient redis = RedisContext.GetClient())
            {
                redis.ExpireEntryAt(key, datetime);
            }
        }
        #endregion
    }
}